//
//  TGLocationItem.m
//  GroupBuy
//
//  Created by wwj on 15/11/2.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGLocationItem.h"
#import "TGDataTool.h"
#import "TGCityListController.h"
#import "TGNavigationController.h"
#import "TGLocationTool.h"

#define kImageScale 0.5

@interface TGLocationItem ()<UIPopoverControllerDelegate>
@property (strong, nonatomic) UIPopoverController *popVC;
@property (strong, nonatomic) UIActivityIndicatorView *indicator;
@end

@implementation TGLocationItem

+ (instancetype)locationItem{
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
        MyLog(@"-----------");
    }
    return self;
}

- (void)initView{
    
    self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    
    [self setTitle:@"定位中" forState:UIControlStateNormal];
    self.titleLabel.font = [UIFont systemFontOfSize:16];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
    [self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    self.imageView.contentMode = UIViewContentModeCenter;
    
    [self addTarget:self action:@selector(onClickLocation) forControlEvents:UIControlEventTouchDown];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cityChangeNotification) name:kCityChange object:nil];
    
    //添加圈圈
    if (_indicator == nil) {
        _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        _indicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        CGFloat x = kDockW * 0.5;
        CGFloat y = 22;
        _indicator.center = CGPointMake(x, y);
        [_indicator startAnimating];
        [self addSubview:_indicator];
        [TGLocationTool shareLocationTool];
    }
}

#pragma -mark 城市改变通知
- (void)cityChangeNotification{
    TGCityModel *city = [TGDataTool shareDataTool].currentCity;
    [self setTitle:city.name forState:UIControlStateNormal];
    [_popVC dismissPopoverAnimated:YES];
    self.enabled = YES;
    
    [_indicator stopAnimating];
    [_indicator removeFromSuperview];
    _indicator = nil;
    // 1.设置内部的图片
    [self setIcon:@"ic_district" selectIcon:@"ic_district_hl"];
}

#pragma -mark 点击定位按钮
- (void)onClickLocation{
    self.enabled = NO;
    
    TGCityListController *cityVC = [[TGCityListController alloc] init];
    UIPopoverController *popVC = [[UIPopoverController alloc] initWithContentViewController:cityVC];
    _popVC = popVC;
    _popVC.delegate = self;
    _popVC.popoverContentSize = CGSizeMake(320, 480);
    [_popVC presentPopoverFromRect:self.bounds inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
    self.enabled = YES;
}

-(CGRect)imageRectForContentRect:(CGRect)contentRect{
    CGFloat w = contentRect.size.width;
    CGFloat h = contentRect.size.height * kImageScale;
    return CGRectMake(0, 0, w, h);
}

-(CGRect)titleRectForContentRect:(CGRect)contentRect{
    CGFloat y = contentRect.size.height * (1 - kImageScale);
    CGFloat w = contentRect.size.width;
    CGFloat h = contentRect.size.height - y;
    return CGRectMake(0, y, w, h);
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
